package com.hao.order.app.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

/**
 * @author haoge
 * @version 1.0
 * @date 2023/5/24 20:56
 */
@Slf4j
public class SendMessageToMq {
    public static void sendDelayMsg(String topic, byte[] body) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        DefaultMQProducer producer = new DefaultMQProducer("order");
        producer.setNamesrvAddr("localhost:9876");
        producer.setRetryTimesWhenSendFailed(3);
        producer.setSendMsgTimeout(5000);

        producer.start();
//messageDelayLevel = 1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m
//1h 2h 1d
        Message msg = new Message(topic, body);
        msg.setDelayTimeLevel(16);
        SendResult send = producer.send(msg);

        log.info("send message to MQ"+send.getSendStatus());
    }
}
